<?php

namespace app\admin\controller;

use app\admin\service\UserService;
use support\spark\model\user\User;
use support\spark\model\user\UserLabel;
use support\Request;
use support\View;

class UserController extends Curd
{

    /**
     * 获取列表
     */
    public function index()
    {
        if (request()->isAjax()) {
            $userService = new UserService();
            $res = $userService->getList(request()->all());
            return sparkSuccess($res);
        }
        $userService = new UserService();
        View::assign($userService->buildBaseParam());
        $labelModel = new UserLabel();
        $labelList = $labelModel->getAllList([], ['id as value', 'name'], ['id' => 'asc']);
        View::assign([
            'label' => json_encode($labelList)
        ]);
        return view('user/index');
    }

    /**
     * 添加
     */
    public function add()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();
            $userService = new UserService();
            $res = $userService->addUser($param);
            return sparkSuccess($res);
        }
    }

    /**
     * 编辑
     */
    public function edit()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();

            $userService = new UserService();
            $res = $userService->editUser($param);
            return sparkSuccess($res);
        }

        $id = request()->input('id');
        $userModel = new User();
        View::assign([
            'info' => $userModel->getInfoById($id)
        ]);
        return view('user/edit');
    }

    /**
     * 余额编辑
     */
    public function balance()
    {
        $param = request()->post();
        $userService = new UserService();
        $res = $userService->changeBalance($param);
        return sparkSuccess($res);
    }
}